<template>

  <div class="btns">
    <el-button :icon="Plus" type="primary" @click="open(null)" size="small">添加陪护人员</el-button>
    <el-popconfirm
        confirm-button-text="是"
        cancel-button-text="否"
        :icon="InfoFilled"
        icon-color="#626aef"
        title="是否确定删除？"
        @confirm="confirmEvent">
      <template #reference>
        <el-button :icon="Delete" type="danger" size="small">删除</el-button>
      </template>
    </el-popconfirm>
  </div>
  <el-table :data="tableData.list" style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55"/>
    <el-table-column prop="id" label="id"/>
    <el-table-column prop="name" label="姓名"/>
    <el-table-column prop="avatar" label="头像">
      <template #default="scope">
        <el-image :src="scope.row.avatar"
                  style="width: 50px;height: 50px"/>
      </template>
    </el-table-column>
    <el-table-column prop="sex" label="性别">
      <template #default="scope">
        <el-tag v-if="scope.row.sex==1" type="success">男</el-tag>
        <el-tag v-else type="danger">女</el-tag>
        <el-tag v-else type="warning">未知</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="age" label="年龄"/>
    <el-table-column prop="mobile" label="手机号"/>
    <el-table-column prop="active" label="生效状态">
      <template #default="scope">
        <el-tag v-if="scope.row.active==1" type="success">生效</el-tag>
        <el-tag v-else type="danger">失效</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="create_time" label="创建时间">
      <template #default="scope">
        <div class="flex-box">
          <el-icon>
            <Lock/>
          </el-icon>
          <span style="padding-left: 10px">{{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--  分页-->
  <div class="pagination-info">
    <el-pagination
        v-model:current-page="paginationData.pageNum"
        :page-size="paginationData.pageSize"
        :background="false"
        size="small"
        :total="tableData.total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="total, prev, pager, next"
    />
  </div>
  <!--  添加弹窗-->
  <el-dialog
      v-model="dialogFormVisable"
      :before-close="beforeClose"
      title="陪护士添加"
      width="500px">
    <el-form
        ref="formRef"
        :rules="rules"
        label-width="100px"
        label-position="left"
        :model="form"
    >
      <el-form-item v-show="false" prop="id">
        <el-input v-model="form.id"/>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-button v-if="!form.avatar" type="primary" @click="dialogImgVisable=true">点击上传</el-button>
        <el-image
            v-else
            :src="form.avatar"
            style="width: 100px;height: 100px"

        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option label="男" value="1"/>
          <el-option label="女" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input-number v-model="form.age" :min="18" :max="50" placeholder="请选择年龄"/>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号"/>
      </el-form-item>
      <el-form-item label="生效状态" prop="active">
        <el-radio-group v-model="form.active">
          <el-radio :value="1">生效</el-radio>
          <el-radio :value="0">失效</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <!--  按钮   -->
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确 定</el-button>
      </div>
    </template>
  </el-dialog>
  <!--  图片上传弹窗 -->
  <el-dialog
      v-model="dialogImgVisable"
      title="选择图片"
      width="680"
      :before-close="beforeClose">
    <div class="image-list">
      <div v-for="(item,index) in fileList" :key="item.name" class="img-box" @click="selectIndex=index">
        <div v-if="index == selectIndex" class="select">
          <el-icon color="#fff">
            <Check/>
          </el-icon>
        </div>
        <el-image
            :src="item.url"
            style="width: 148px;height: 148px"
        />
      </div>
    </div>
    <!--  按钮   -->
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="dialogImgVisable=false">取 消</el-button>
        <el-button type="primary" @click="confirmImage">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {ref, reactive, onMounted, nextTick} from 'vue'
import {Check, Lock, Plus, InfoFilled, Delete} from "@element-plus/icons-vue";
import {photoList, companion, companionList, deleteCompanion} from '../../../api'
import {ElMessage} from "element-plus";

// 加载数据
onMounted(() => {
  getListData()
  photoList().then(({data}) => {
    fileList.value = data.data
  })
})
// 分页数据
const paginationData = reactive({
  pageNum: 1,
  pageSize: 10
})
// 列表数据
const tableData = reactive({
  list: [],
  total: 0
})
// 获取列表的方法
const getListData = () => {
  companionList(paginationData).then(({data}) => {
    const {list, total} = data.data;
    list.forEach(item => {
      item.create_time = new Date(item.create_time).toLocaleString()
    })
    tableData.list = list
    tableData.total = total
  })
}
// 修改当前页数
const handleCurrentChange = (val) => {
  paginationData.pageNum = val
  getListData()
}
// 修改每页显示条数
const handleSizeChange = (val) => {
  paginationData.pageSize = val
  getListData()
}
// 多选框
const selection = ref([])
const handleSelectionChange = (val) => {
  selection.value = val
}
// 删除事件
const confirmEvent = () => {
  if (selection.value.length === 0) {
    return ElMessage({message: '请选择需要删除的数据', type: 'warning'})
  }
  deleteCompanion({id: selection.value}).then(({data}) => {
    console.log(data)
    if (data.code === 10000) {
      ElMessage({message: '删除成功', type: 'success'})
      getListData()
    } else {
      ElMessage({message: data.message, type: 'error'})
    }
  })


}
// 弹窗开启关闭标记
const dialogFormVisable = ref(false)

// 打开弹窗
function open(rowData = {}) {
  dialogFormVisable.value = true
  // 赋值 表单数据
  nextTick(() => {
    // 如果编辑
    if (rowData) {
      Object.assign(form, rowData)
    }
  })
}

// 关闭弹窗
function beforeClose() {
  dialogFormVisable.value = false
  // 重置表单数据
  formRef.value.resetFields();
}


// 表单
// 表单ref  ref 是一个对象，通过 formRef.value 可以获取到表单实例并调用其方法
const formRef = ref()
// 表单数据
const form = reactive({
  id: '', // 陪护员id
  mobile: '', // 手机号
  active: 1, // 生效状态 1有效 0无效
  age: 28,
  avatar: '', // 头像
  name: '', // 姓名
  sex: '' // 性别
})
// 表单验证 rules rules 是一个对象，key 是表单项的 prop 属性值，value 是用来验证该表单项的规则
const rules = reactive({
  name: [
    {required: true, message: '请输入名称', trigger: 'blur'}
  ],
  avatar: [
    {required: true, message: '请选择头像'}
  ],
  sex: [
    {required: true, message: '请选择性别', trigger: 'change'},
  ],
  mobile: [
    {required: true, message: '请输入手机号', trigger: 'blur'},
  ]
})
// 确定
const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      companion(form).then(({data}) => {
        if (data.code === 10000) {
          ElMessage.success(data.message)
          beforeClose()
          getListData()

        } else {
          ElMessage.error(data.message)
        }
      })
    } else {
      console.log('error submit!!')
    }
  })
}


// 图片上传弹窗
// 开启关闭标记
const dialogImgVisable = ref(false)
// 图片列表
const fileList = ref([])
// 选中图片
const selectIndex = ref(0)

function openDialog() {
  dialogImgVisable.value = true
}

// 确定
const confirmImage = () => {
  // 将选中图片赋值给form
  form.avatar = fileList.value[selectIndex.value].url
  // 关闭弹窗
  dialogImgVisable.value = false

}
</script>
<style lang="scss" scoped>
.btn {
  padding: 10px 0 10px 10px;
  background-color: #fff;
}

.image-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  margin: 10px;

  .img-box {
    position: relative;

    .select {
      position: absolute;
      left: 0px;
      top: 0px;
      background: green;
      z-index: 10;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}

.pagination-info {
  display: flex;
  justify-content: flex-end;
}

.flex-box {
  display: flex;
  align-items: center;
}
</style>
